package cn.hxzy.kaoshi.controller;


import cn.hxzy.common.domain.ClassTable;
import cn.hxzy.common.response.R;
import cn.hxzy.kaoshi.entity.SysExam;
import cn.hxzy.kaoshi.service.ISysExamService;
import cn.hxzy.teacher.service.TeacherService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.lettuce.core.ScriptOutputType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 考试管理 前端控制器
 * </p>
 *
 * @author tyqw
 * @since 2024-12-13
 */
@RestController
@RequestMapping("/sysExam")
public class SysExamController {
    @Autowired
    private ISysExamService iSysExamService;
    @Autowired
    private TeacherService teacherService;

   //根据班级编号查询考试信息
    @RequestMapping("/fselectAll")
    public R fselectAll(@RequestBody Map<String, Object> map) {
        PageInfo<Map> maps = iSysExamService.fselectAll(map);
        return R.success(maps);
    }


    @GetMapping("/findAllExamResults/{Datatime}")
    public R findAllExamResults(@PathVariable Integer Datatime) {
        SysExam sysExam = iSysExamService.fselectAll2(Datatime);
        // 获取 startTime 和 endTime
        String startTime = sysExam.getStartTime();
        String endTime = sysExam.getEndTime();

        // 判断当前时间是否在 startTime 和 endTime 之间
        boolean isWithinTimeRange = isTimeWithinRange(startTime, endTime);

        return R.success(isWithinTimeRange);
    }

    public boolean isTimeWithinRange(String startTime, String endTime) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            Date startDate = dateFormat.parse(startTime);
            Date endDate = dateFormat.parse(endTime);
            Date currentDate = new Date();

            return currentDate.after(startDate) && currentDate.before(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }
    //根据学生id查询班级
    @GetMapping("/xueshichaban/{userid}")
    public R xueshichaban(@PathVariable("userid") Long userid) {
        Map<String,Object> xueshichaban = iSysExamService.xueshichaban(userid);
        return R.success(xueshichaban);
    }

    @PostMapping("/findExaminations")
    public R findExaminations(@RequestBody Map<String,Object> map) {
        PageInfo<Map> examinations = iSysExamService.findExaminations(map);
        return R.success(examinations);
    }

    @GetMapping("/findClass")
    public R    findClass() {
        List<Map> maps = iSysExamService.findClass();
        return R.success(maps);
    }
    @PostMapping("/tianjia")
    public R tianjia(@RequestBody SysExam sysExam) {
        iSysExamService.insertOrUpdate(sysExam);
        return R.success(true);
    }
    @GetMapping("/deleteExam/{examId}")
    public R deleteExam(@PathVariable("examId") Long examId) {
        iSysExamService.deleteExam(examId);
        return R.success(true);
    }

    @GetMapping("/findAllClassroom")
    public R findAllClassroom(){
        List<ClassTable> allClassroom = teacherService.findAllClassroom();
        return R.success(allClassroom);
    }
}
